<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>确认订单</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
<link rel="stylesheet" type="text/css" href="/static/css/shoujisc.css">
<script type="text/javascript" src="/static/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="/static/js/woxiangyao.js"></script>
<script type="text/javascript" src="/static/js/TouchSlide.1.1.js"></script>
<script type="text/javascript" src="/static/js/foot.js"></script>
<script type="text/javascript" src="/static/js/myinit.js"></script>
<script type="text/javascript" src="/static/js/xujianbanner.js"></script>
<script type="text/javascript" src="/static/js/cook_shop_cart.js"></script>
<style type="text/css">
.mypageback {
	float: left;
}
.mynormalh3 {
	line-height: inherit !important;
}
.myzhifumima {
	border-left: grey 1px solid;
	padding-left:1px;
	width: 30px;
	height: 36px;
	display: inline-block;
}

.myzhifumimatian{
	background:url(/static/img/dian.png) no-repeat center;
}
</style>
</head>

<body >
<div class="sjsc-title1" style="height:45px">
    <h3 class="sjsc-t1l f-l" style="height:45px;line-height:45px"><a class="mypageback"><span><</span>确认订单</a></h3>
    <div style="clear:both;"></div>
</div>
<div id="add_choose">
    <dl class="drdd-info1">
        <dt> <a  >
            <p>选择添加收货地址</p>
            </a> </dt>
        <dd><a href="#">></a></dd>
        <div style="clear:both;"></div>
    </dl>
</div>
<div style="position:fixed;width:100%;height:100%;background-color:rgba(0,0,0,0.3);z-index:99;top:0;left:0px;text-align:center;display:none;font-size:14px" id="addr_dialog">
    <ul style="width: 70%; background-color: white; display: inline-block; margin-top: 10%; border-radius: 5px; padding-top: 10px; padding-bottom: 10px;overflow-y: auto;height:70%;font-size:14px" id="dizhi_list">
    </ul>
</div>
<div class="drdd-info2">
    <p class="p1 f-l">共<span id="all_count"></span>件商品</p>
    <p class="p2 f-r">总计：<span>￥<span id="all_price"></span></span></p>
    <div style="clear:both;"></div>
</div>
<div class="drdd-info3" id="allgoods"> </div>
<div class="drdd-info4" style="height:auto">
    <p>配送方式</p>
    <a href="#">快递 包邮<span>></span></a>
    <div style="clear:both;"></div>
</div>
<div class="drdd-info4" style="height:auto;display:none;" id="youhuibody">
    <div  style="color:#fdb633;font-size:14px">当前优惠<a id="youhui" style="color:#fdb633;"></a></div>
    <div style="clear:both;"></div>
</div>
<div class="drdd-info2">
    <p class="p2 f-r">订单总计：<span>￥<span id="real_price"></span></span></p>
    <div style="clear:both;"></div>
</div>
<dl class="drdd-info5">
    <dt style="margin-top:10px">
        <h3>支付方式</h3>
    </dt>
    <dd>
        <ul class="drdd-ul1" style="text-align:center;">
            <li style="width:85%;background:#b4bd63;color:white;height:40px;margin-top:10px;line-height:40px;text-align:center;display:inline-block;border-radius:5px;font-size:16px;margin-bottom:10px" id="weixinpayit">微信支付</li>
            <!--
            	<li style="width:85%;background:#b4bd63;color:white;height:40px;line-height:40px;text-align:center;display:inline-block;border-radius:5px;font-size:14px;margin-bottom:10px"><label><input type="radio" name="zhifu" id="inweixin"/>微信支付</label></li>
				<li style="width:85%;background:#fdb633;color:white;height:40px;line-height:40px;text-align:center;display:inline-block;border-radius:5px;font-size:14px;margin-bottom:10px"><label><input type="radio" name="zhifu" id="incash"/>现金余额</label></li>
                
                -->
            <li style="width:85%;background:#fdb633;color:white;height:40px;line-height:40px;text-align:center;display:inline-block;border-radius:5px;font-size:16px;margin-top:10px;" id="incash">我的余额支付（￥<span id="yue"></span>）</li>
        </ul>
    </dd>
</dl>

<div id="zhifudialog_container" style="position:absolute;top:0px;left:0px;height:100%;background-color:rgba(0,0,0,0.25);width:100%;display:none;;text-align:center">
    <div id="zhifu_dialog_body" style="background-color:white;border-radius:10px;display:inline-block;width:320px;max-width:80%">
        <div style="height:40px;line-height:40px;border-bottom:green 1px solid;font-size:large;position:relative">
            <div style="height:40px;line-height:40px;color:#CCC;position:absolute;left:20px" id="zhifu_dialog_cancel">X</div>
            <div style="height:40px;line-height:40px">请输入支付密码</div>
        </div>
        <div style="margin-top:25px">我的余额支付</div>
        <div style="height: 50px; line-height: 50px; font-size: xx-large;">￥<span id="zhifu_dialog_jine">11.00</span></div>
        <div style="position: relative; min-height: 60px;;margin-top:10px">
            <input id="password11" style="position:absolute;top:0px;left:-1000px" autofocus type="password">
            <div style="position: absolute; left: 1em; right: 1em; height: 36px;" id="fakebody">
                <div style="text-align:center">
                    <div style="border: 1px solid gray; display: inline-block; height: 36px;">
                        <div class="myzhifumima" style="border-left:none"></div>
                        <div class="myzhifumima"></div>
                        <div class="myzhifumima"></div>
                        <div class="myzhifumima"></div>
                        <div class="myzhifumima"></div>
                        <div class="myzhifumima"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!--
    <div style="text-align:center"><button class="drdd-btn" id="cmf" style="background:none;border:1px solid #941d5c;width:90%;display:inline-block;border-radius:5px;color:#941d5c;height:40px;line-height:40px">确认支付</button></div>
    --> 
<script type="text/javascript">
		var openid =getUrldata('OPENID');
		var custadr=-1;
		var orderid=-1;
		
		
		var addrid=$.cookie("ADDRID");
		if(addrid==''||addrid==undefined){
			addrid='';
		}
		
		function saveaddrid(id){
			addrid=id;
			$.cookie("ADDRID",addrid);
		}
		
		//优惠id
		var yhid="";
		
		
		
		
		$.ajaxSettings.async = false; 
		$.ajaxSettings.cache=false;
		$('.mypageback').click(function(){
			window.history.go(-1);
		});
		
		$(document).ready(function(){
			initdizhilist(0);
			initCart();
			getyouhuijuan();
			getyue();
		});
		function initdizhilist(bb){
			var url ='/webserver/api/v1/custadr/?format=json&wx_id='+openid;
			$.getJSON(url,function(rs){
				var objects=rs.objects;
				if(objects!=null&&objects!=undefined&&objects.length>0&&$('#dizhi_list').find('li').length==0){
					$('#dizhi_list').append('<li><a onclick="toUrl(\'xinzengshouhuo.html?OPENID='+openid+'\')" style="color:blue !important" >添加新的收货地址</a></li>');
					$.each(objects,function(index,item){
						 
						$('#dizhi_list').append('<li style="border-bottom:grey solid 1px;line-height:20px;text-align:left;padding-left:20px;padding-top:10px;padding-bottom:10px" id="'+item.id+'">'+item.cityname+item.detailAdrinfo+'</li>');
					
					});
					
					if(addrid!=''){
						setAddr(addrid);
						
					}else if(objects.length!=0){
						$('#add_choose').empty();
						$('#add_choose').append('<dl class="drdd-info6" style="height:auto"><dt><p><span class="f-l" style="font-size:14px">收货人：'+objects[0].custname+'</span><span class="f-r" style="font-size:14px">联系电话：'+objects[0].phoneno+'</span><div style="clear:both;"></div></p><p style="font-size:14px">收货地址：'+objects[0].cityname+objects[0].detailAdrinfo+'</p></dt><dd><a href="#">></a></dd><div style="clear:both;"></div</dl>');
						custadr=objects[0].id;
					}
					$('#dizhi_list li').click(function(){
						var id=$(this).attr('id');
						if(id!=undefined&&id!=null&&id!='undefined'&&id!='null'){
							saveaddrid(id);
							setAddr(id);
						}
						
					});
					$('#dizhi_list').append('<li><button class="drdd-btn" onclick="addresscancel()" style="margin-top:20px;">取消</button></li>');
					
					
				}
				if(bb==1){
					if($('#dizhi_list').find('li').length==0){
						window.location.href="xinzengshouhuo.html?OPENID="+openid;
					}
				}
				
				
			});
		}
		
		function addressshow(){
			 
			if($('#dizhi_list').find('li').length==0){
				window.location.href="xinzengshouhuo.html?OPENID="+openid;
			}else{
				initdizhilist(1);
				$('#addr_dialog').show();
			}
			
		}
		
		function addresscancel(){
			$('#addr_dialog').hide();
			
		}
		
		function toUrl(url){
			document.location.href=url;
		}
		
		$('#add_choose').click(function(){
			addressshow();
		
		});
		
		
		function setAddr(id){
			var aa={};
			var url ="/webserver/api/v1/custadr/?format=json&id="+id;
			$.getJSON(url,function(rs){
				var objects=rs.objects;
				if(objects.length==1){
					var ads=objects[0];
					$('#add_choose').empty();
					$('#add_choose').append('<dl class="drdd-info6" style="height:auto"><dt><p><span class="f-l" style="font-size:14px">收货人：'+objects[0].custname+'</span><span class="f-r" style="font-size:14px">联系电话：'+objects[0].phoneno+'</span><div style="clear:both;"></div></p><p style="font-size:14px">收货地址：'+objects[0].cityname+objects[0].detailAdrinfo+'</p></dt><dd><a href="#">></a></dd><div style="clear:both;"></div</dl>');
					
					$('#addr_dialog').hide();
					custadr=id;
				}
			});
		}
		
		
		
		function initCart(){
			var cart = cartHelper.getCart();
			var arr = cart.items;
			$.each(arr,function(index,item){
				if(index!=0){
					$('#allgoods').append('<hr style="margin-top:0px;margin-bottom:0px;padding-bottom:0px;padding-top:0px;"></hr>');
				}
				$('#allgoods').append('<div class="drdd-if3tu f-l" style="width:88px;height:88px"><img src="'+item.img+'"></div><h3 class="drdd-h31 f-l mynormalh3" style="font-size:14px">'+item.name+'<p style="color:grey">'+item.xiangqing+'</p></h3><p class="drdd-p1 f-r" style="font-size:14px">￥'+(item.price*0.01).toFixed(2)+'x'+item.count+'</p><div style="clear:both;"></div>');
				
			});
			
			
			$('#all_count').html(cart.count);
			$('#all_price').html((cart.total*0.01).toFixed(2));
		
		
		}
		
		function addAorder(isweixin){
			if(custadr==-1){
				toast("请选择收件人地址");
				return;
			}
			var url = "/shop/api/addorder/";
			var aa={};
			aa.openid=openid;
			aa.custadr=custadr;
			aa.beizhu="";
			aa.detail=[];
			if(yhid!=''){
				aa.deductionitem=yhid;
			}else{
				aa.deductionitem=0;
			}
			
			var cart = cartHelper.getCart();
			var arr = cart.items;
			if(arr.length==0){
				toast('购物车为空不能支付!');
				return;
			}
			$.each(arr,function(index,item){
				var bb={};
				bb.id=item.goodid;
				bb.count=item.count;
				bb.xiangqing=item.xiangqing;
				bb.price=item.price*item.count*0.01;
				aa.detail.push(bb);
			});
			
			console.log(aa);
			dopost(url,aa,function(rs){
				if(rs.orderid!=null&&rs.orderid!=undefined&&rs.orderid!='null'&&rs.orderid!='undefined'){
					orderid = rs.orderid;
					cartHelper.Clear();
					if(isweixin){
						pay();
						
						
					}else{
						//使用 现金支付按钮 代替弹出支付对话框
						//$('#incash').click();
						 
						 
					}
					
					
					
				}
				
				 
			});
			
			 
		
		
		}
		
		
		
		function pay(){
			if(orderid!=-1){
				var url="/weixin/getpayparm/";
				$.post(url,{"orderid":orderid},function(aa){
					if (typeof WeixinJSBridge == "undefined"){
						if( document.addEventListener ){
							document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
						}else if (document.attachEvent){
							document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
							document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
						}
					}else{
						onBridgeReady(aa);
					}
				}, "json");
			}
		 
		}
		/*
		$('#cmf').click(function(){
			if(orderid==-1){
				addAorder();
			}else{
				if($('#incash').is(':checked')){
					payincash();
						
				}else if($('#inweixin').is(':checked')){
					pay();
				}else{
					alert('尚未选择支付方式');
				}
			}
			
		});
		*/
		
		$('#weixinpayit').click(function(e) {
			
			if(orderid==-1){
				addAorder(true);
			}else{
				//微信支付
				pay();
				 
			}
            
        });
		
		$('#incash').click(function(e) {  
			if(!haspasswordornot()){
				return;
			};
			var	yue=parseFloat($('#yue').html());
				 
		 
				
			
			 
			  
			 
			var real_price=$('#real_price').html();
			
			if(real_price>yue){
				
				var bb=[];
				
				var aa={};
				aa.NAME="取消";
				aa.ACTION=function(){
					
				};
				bb.push(aa);
				
				aa={};
				aa.NAME="去充值";
				aa.ACTION=function(){ document.location.href="xianjinzhanghu.html?OPENID="+openid;};
				bb.push(aa);
				toastwithactions("余额不足!?",bb);
				return ;
			}
			
			
			
			
			
			if(orderid==-1){
				zhifudialogshow();
				addAorder(false);
			}else{
				//余额支付
				zhifudialogshow();
				 
						
				
				
				
				 
			}
			 
            
            
        });
		
		
		function onBridgeReady(aa){
			/*{"package": "prepay_id=wx201608281547444714ea5d940343456008", "timeStamp": 1472370461, "signType": "MD5"
, "paySign": "670F60B78D4DA7AB66A07135A0AB5C01", "appId": "wx1ef1259eec8b148d", "nonceStr": "PXMmWrTxEKDaqrFD"
}*/
			 
			WeixinJSBridge.invoke('getBrandWCPayRequest', {
				"appId" : aa.appId,     //公众号名称，由商户传入     
				"timeStamp" : aa.timeStamp+"",         //时间戳，自1970年以来的秒数     
				"nonceStr"  :  aa.nonceStr, //随机串     
				"package"  :  aa.package,
				"signType"  :  aa.signType,         //微信签名方式 : 
				"paySign"  :  aa.paySign //微信签名 
			},
			function(res){     
			 
				if(res.err_msg == "get_brand_wcpay_request:ok" ) {
				
					toastwithaction('支付成功',function(){
						document.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ef1259eec8b148d&redirect_uri=http%3a%2f%2fwww.tiantianhw.cn%2fmshop%2fwodedingdan.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
					});
					
				}     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
			});
		}
		
		function payincash(){
			var urll="/weixin/payorder/";
			
			/*
			$.post(url,,function(aa){
				console.log("/weixin/payorder/"+rs);
				if(aa==true){
					toastwithaction('支付成功',function(){
						document.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ef1259eec8b148d&redirect_uri=http%3a%2f%2fwww.tiantianhw.cn%2fmshop%2fwodedingdan.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
					});
				}else{
					toast('账户余额不足!');
					
				}
			}, "json");
			*/
			
			$.ajax({
				type: 'POST',
				url: urll,
				data: {"orderid":orderid},
				success:function(aa){
					if(aa.code=='true'){
						toastwithaction('支付成功',function(){
						document.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ef1259eec8b148d&redirect_uri=http%3a%2f%2fwww.tiantianhw.cn%2fmshop%2fwodedingdan.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
						});
					}else{
						toast('账户余额不足!');
					
					}
				},
				dataType: "json",
				error: function(){
					toast('账户余额不足!');
				}
			});
			
			
			 
		} 
 
		
		
		
		function getyouhuijuan(){
			var url="/webserver/api/v1/deduction/?format=json";
			$.getJSON(url,function(rs){
				
				var objects=rs.objects;
				var cart = cartHelper.getCart();
				var total=cart.total;
				
				var temp_max=-1;
				var temp_id=-1;
				var bb;
				$.each(objects,function(index,item){
					var condi_v=parseInt(item.condi_value*100);
					if(condi_v>temp_max&&!(total<condi_v)){
						temp_max=condi_v;
						temp_id=item.id;
						bb=item;
					}
				});
				console.log(temp_id);
				if(temp_id!=-1){
					addAyouhuiquan(temp_id);
					$('#youhui').html(bb.name);
					$('#youhuibody').show();
					
					
					
					$('#real_price').html(((cart.total-parseInt(bb.deduct_value*100))/100).toFixed(2));
				}else{
					$('#youhuibody').hide();
					$('#real_price').html((cart.total/100).toFixed(2));
				}
				
				
			},'json');
		}
		
		
		function addAyouhuiquan(id) {
			var url = "/webserver/api/v1/deductionitem/";
			var aa = {};
			aa.openid = openid;
			aa.deduction="/webserver/api/v1/deduction/"+id+"/";
			aa.id = id;
			dopost(url, aa, function(rs) {
				//pay(rs.id);
				yhid=rs.id;
			});
		}
		
		function getyue(){
			var url="/webserver/api/v1/user/?format=json&openid="+openid;
			$.getJSON(url,function(rs){
				var object=rs.objects[0];
			
				var balance=object.balance;
				balance=parseFloat(balance).toFixed(2);
				$('#yue').html(balance);
				
				
			});
	
			
		}
		//在支付前判断  如果没有密码先去设置密码 , 如果有的话 弹出的支付密码输入框....
		
		function haspasswordornot(){
			var isok=true;
			var url="/webserver/api/v1/user/?format=json&openid="+openid;
			$.getJSON(url,function(rs){
				if(rs.objects.length!=0){
				var object=rs.objects[0];
			
				var password=object.password;
				if(password==null||password=='null'||password=='undefined'||password==undefined||password==''){
					alert('还没有设置密码请先设置会员密码!');
					document.location.href="zhuce.html?OPENID="+openid;
					isok= false;;
				}
				}else{
					 
				var bb=[];
				
				var aa={};
				aa.NAME="取消";
				aa.ACTION=function(){
					
				};
				bb.push(aa);
				
				aa={};
				aa.NAME="去注册";
				aa.ACTION=function(){  document.location.href="zhuce.html?OPENID="+openid;};
				bb.push(aa);
				toastwithactions("还不是会员!?",bb);
				isok= false;
				
				}
				
			});
			return isok;
			
			
			
		}
		
		 
		
		$('#zhifu_dialog_cancel').click(function(e) {
			$('#zhifudialog_container').hide();
            
        });
		
		//
		$('#password11').keyup(function(e) {
			var aa=$(this).val();
			var len=aa.length;
			$('.myzhifumima').each(function(index, element) {
                if(index<len){
					$(this).addClass('myzhifumimatian');
				}else{
					$(this).removeClass('myzhifumimatian');
				}	
            });
			if(len==6){
				$('#password11').blur();
				checkmima(aa);
			}
        });
		
		function checkmima(aa){
			var url="/webserver/api/v1/user/?format=json&openid="+openid;
			
			$.getJSON(url,function(rs){
				$('#zhifudialog_container').hide();
				var object=rs.objects[0];
			
				var password=object.password;
				if(aa==password){
					
					payincash();
				}else{
					toast('密码错误');
					
				}
				
				
				
			});
		
		}
		
		function zhifudialogshow(){
			$('#zhifudialog_container').show();
			$('#password11').focus();
			$('#password11').trigger('focus');
			$('.myzhifumima').removeClass('myzhifumimatian');
			$('#password11').val('');
			setTimeout(function(){
				
				$('#zhifu_dialog_body').css('margin-top',$('body').scrollTop()+10);
			},100);
		
		}
		
		$('.myzhifumima').click(function(e) {
			 
          $('#password11').focus();
			setTimeout(function(){
				
				$('#zhifu_dialog_body').css('margin-top',$('body').scrollTop()+10);
			},100);
        });
		
		 
		 
	</script>
</body>
</html>
